﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace 测试Stream
{
    class Program
    {
        static void Main(string[] args)
        {

            float t = 3.44456f;
            Console.WriteLine(Math.Round(t,2));
            Console.WriteLine(Math.Round(t, 3));
            Console.WriteLine(Math.Round(t, 4));

            //string text = "abc\nedf\n";
            //using(TextReader tr = new StringReader(text))
            //{
            //    string content = tr.ReadToEnd();
            //    Console.WriteLine(content);
            //    tr.Close();
            //}

            //using(TextReader tr = new StringReader(text))
            //{
            //    while(tr.Peek() != -1)
            //    {
            //        Console.WriteLine($"peek:{(char)tr.Peek()}");
            //        Console.WriteLine($"read:{(char)tr.Read()}");
            //    }
            //    tr.Close();
            //}

            //Console.WriteLine("=============readBlock=============");

            //using(TextReader tr = new StringReader(text))
            //{
            //    char[] charBuffer = new char[3];
            //    int data = tr.ReadBlock(charBuffer, 0, 3);
            //    Console.WriteLine($"读到的数量:{data}");
            //    for(int i = 0; i < data; ++i)
            //    {
            //        Console.WriteLine(charBuffer[i]);
            //    }

            //    // 再读2个
            //    Console.WriteLine("============================");
            //    int newCount = tr.ReadBlock(charBuffer, 0, 2);
            //    for(int i = 0; i < newCount; ++i)
            //    {
            //        Console.WriteLine(charBuffer[i]);
            //    }
            //    tr.Close();
            //}

            //
            Console.ReadKey();
        }



        /// <summary>
        /// MemoryStream的基本使用
        /// </summary>
        public static void TestStream() {
            string testString = "Stream!Hello world";

            using (MemoryStream ms = new MemoryStream())
            {
                Console.WriteLine($"初始化字符串:{testString}");

                // 如果流可写
                if (ms.CanWrite)
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(testString);
                    ms.Write(buffer, 0, 3);

                    // 
                    Console.WriteLine($"现在Stream.Position为:{ms.Position}");
                    long newPosition = ms.CanSeek ? ms.Seek(3, SeekOrigin.Current) : 0;
                    Console.WriteLine($"重新定位后:{ms.Position}, newPosition:{newPosition}");
                    if (newPosition < buffer.Length)
                    {
                        ms.Write(buffer, (int)newPosition, buffer.Length - (int)newPosition);
                    }

                    // 写完后,将stream的 position设置为0,开始读流中的数据
                    ms.Position = 0;

                    // 设置一个容器来接收流中的数据,长度根据stream的长度来决定
                    Console.WriteLine($"流的长度:{ms.Length}");
                    byte[] readBuffer = new byte[ms.Length];
                    int count = ms.CanRead ? ms.Read(readBuffer, 0, readBuffer.Length) : 0;
                    Console.WriteLine($"count:{count}");

                    string str = Encoding.UTF8.GetString(readBuffer);
                    Console.WriteLine($"str:{str}");
                    ms.Close();
                }
            }
        }
    }

}
